package com.ziytek.web.citizen.config;

import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.TableConstant;
import com.ziytek.web.citizen.mapper.*;
import com.ziytek.web.citizen.pojo.basic.DoorConfig;
import com.ziytek.web.citizen.service.DoorsService;
import com.ziytek.web.citizen.service.SqliteMasterService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author fenghx
 * @version 1.0
 * @className CreateTableConfig
 * Description:
 * @date 2024/4/12 16:46
 */
@Slf4j
@Component
@RefreshScope
@RequiredArgsConstructor
public class CreateTableConfig {
    private final TableMapper tableMapper;
    private final DoorsService doorsService;
    private final BoardConfigMapper boardConfigMapper;
    private final VersionInfoMapper versionInfoMapper;
    private final HistoryTableMapper historyTableMapper;
    private final CommandIssuedMapper commandIssuedMapper;
    private final LocalCapacityMapper localCapacityMapper;
    private final SqliteMasterService sqliteMasterService;
    private final BasicDataBatchMapper basicDataBatchMapper;
    private final OfflineTaskTableMapper offlineTaskTableMapper;


    @PostConstruct
    public void createTableConfigInit() {
        createTableInit();
        sqliteMasterService.addColumnIfNotExists("hasCapacity");
    }

    public void createTableInit() {
        log.info("createTableConfigInit start");
        var sql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,`cabinetNo` VARCHAR(8) NOT NULL," +
                " `boardNo` INTEGER NOT NULL ,`channelNo` bigint NOT NULL ,`stepId` bigint NOT NULL," +
                " `settingsDetail` VARCHAR(200) NOT NULL , `orderNo` bigint NOT NULL ,`expectedTime` bigint NOT NULL ,`hasExecutedTime` bigint NOT NULL," +
                " `finishedStatus` INTEGER NOT NULL , `createTime` bigint NOT NULL , `firstDataTime` bigint NOT NULL , `updateTime` bigint NOT NULL ";
        tableMapper.createTable(TableConstant.TABLE_COMMAND_ISSUED, sql);
        var commandIssued = commandIssuedMapper.findByCommandChannel("2", 1, 1);
        log.info("createTableConfigInit commandIssued {}", commandIssued);

        List<DoorConfig> doors = doorsService.getDoorsConfig();
        for (var door : doors) {
            var boardAddress = boardConfigMapper.queryByDoorListNum(door.getDoor());
            if (boardAddress == null) {
                continue;
            }

            for (var board : boardAddress) {
                var realSql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                        " `batchNo` VARCHAR(48) NOT NULL, " +
                        " `cabinetNo` INTEGER NOT NULL, " +
                        " `boardNo` INTEGER NOT NULL ," +
                        " `channelNo` INTEGER NOT NULL," +
                        " `data` blob NOT NULL, time bigint NOT NULL, " +
                        " `createTime` bigint NOT NULL  ";

                tableMapper.createTable(TableConstant.TABLE_REAL_TIME + board, realSql);
                var indexName = "index_" + TableConstant.TABLE_REAL_TIME + board;
                var indexColumns = "'cabinetNo','boardNo','channelNo','createTime'";
                tableMapper.createIndex(TableConstant.TABLE_REAL_TIME + board, indexName, indexColumns);
            }
        }

        var realStatsSql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " `operateId` VARCHAR(48) NOT NULL, " +
                " `batchNo` VARCHAR(48) NOT NULL, " +
                " `cabinetNo` INTEGER NOT NULL, " +
                " `boardNo` INTEGER NOT NULL ,`channelNo` INTEGER NOT NULL," +
                " `data` blob NOT NULL, time bigint NOT NULL, " +
                " `createTime` bigint NOT NULL  ";

        tableMapper.createTable(TableConstant.TABLE_REAL_TIME_STATS, realStatsSql);
        var indexName = "index_" + TableConstant.TABLE_REAL_TIME_STATS + "_1";
        var indexColumns = "'cabinetNo','boardNo','channelNo','createTime'";
        tableMapper.createIndex(TableConstant.TABLE_REAL_TIME_STATS, indexName, indexColumns);


        if (NumConstant.INT_ZERO == basicDataBatchMapper.existTable(TableConstant.TABLE_BASIC_DATA)) {
            basicDataBatchMapper.createTable();
            basicDataBatchMapper.createTableIndex();
            basicDataBatchMapper.createTableIndexOne();
        }


        if (NumConstant.INT_ZERO == offlineTaskTableMapper.existTable(TableConstant.TABLE_OFFLINE_TASK_TABLE)) {
            offlineTaskTableMapper.createTable();
            offlineTaskTableMapper.createTableIndex();
            offlineTaskTableMapper.createTableIndexOne();
            offlineTaskTableMapper.createTableIndexTwo();
        }


        if (NumConstant.INT_ZERO == versionInfoMapper.existTable(TableConstant.TABLE_PROGRAM_VERSION_INFO)) {
            versionInfoMapper.createTable();
            versionInfoMapper.createTableIndex();
            versionInfoMapper.createTableIndexOne();
            versionInfoMapper.createTableIndexTwo();
        }

        if (NumConstant.INT_ZERO == localCapacityMapper.existTable(TableConstant.TABLE_LOCAL_CAPACITY)) {
            localCapacityMapper.createTable();
            localCapacityMapper.createTableIndex();
            localCapacityMapper.createTableIndexOne();
            localCapacityMapper.createTableIndexTwo();
            localCapacityMapper.createTableIndexThree();
            localCapacityMapper.createTableIndexFour();
        }

        log.info("createTableConfigInit end");
    }

    public void historyCreateTableInit() {
        log.info("history createTableConfigInit start");
        var sql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,`cabinetNo` VARCHAR(8) NOT NULL," +
                " `boardNo` INTEGER NOT NULL ,`channelNo` bigint NOT NULL ,`stepId` bigint NOT NULL," +
                " `settingsDetail` VARCHAR(200) NOT NULL , `orderNo` bigint NOT NULL ,`expectedTime` bigint NOT NULL ,`hasExecutedTime` bigint NOT NULL," +
                " `finishedStatus` INTEGER NOT NULL , `createTime` bigint NOT NULL , `firstDataTime` bigint NOT NULL , `updateTime` bigint NOT NULL ";
        historyTableMapper.createTable(TableConstant.TABLE_COMMAND_ISSUED, sql);
        //var commandIssued = commandIssuedMapper.findByCommandChannel("2", 1, 1);
        ///log.info("createTableConfigInit commandIssued {}", commandIssued);

        List<DoorConfig> doors = doorsService.getDoorsConfig();
        for (var door : doors) {
            var boardAddress = boardConfigMapper.queryByDoorListNum(door.getDoor());
            if (boardAddress == null) {
                continue;
            }

            for (var board : boardAddress) {
                var realSql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                        " `batchNo` VARCHAR(48) NOT NULL, " +
                        " `cabinetNo` INTEGER NOT NULL, " +
                        " `boardNo` INTEGER NOT NULL ," +
                        " `channelNo` INTEGER NOT NULL," +
                        " `data` blob NOT NULL, time bigint NOT NULL, " +
                        " `createTime` bigint NOT NULL  ";

                historyTableMapper.createTable(TableConstant.TABLE_REAL_TIME + board, realSql);
                var indexName = "index_" + TableConstant.TABLE_REAL_TIME + board;
                var indexColumns = "'cabinetNo','boardNo','channelNo','createTime'";
                historyTableMapper.createIndex(TableConstant.TABLE_REAL_TIME + board, indexName, indexColumns);
            }
        }

        var realStatsSql = " `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " `operateId` VARCHAR(48) NOT NULL, " +
                " `batchNo` VARCHAR(48) NOT NULL, " +
                " `cabinetNo` INTEGER NOT NULL, " +
                " `boardNo` INTEGER NOT NULL ,`channelNo` INTEGER NOT NULL," +
                " `data` blob NOT NULL, time bigint NOT NULL, " +
                " `createTime` bigint NOT NULL  ";

        historyTableMapper.createTable(TableConstant.TABLE_REAL_TIME_STATS, realStatsSql);
        var indexName = "index_" + TableConstant.TABLE_REAL_TIME_STATS + "_1";
        var indexColumns = "'cabinetNo','boardNo','channelNo','createTime'";
        historyTableMapper.createIndex(TableConstant.TABLE_REAL_TIME_STATS, indexName, indexColumns);


        log.info("history createTableConfigInit end");
    }
}
